package com.express.controller;

import com.express.entity.Result;
import com.express.entity.StatusCode;
import com.express.service.ExpressService;
import com.express.service.UserService;
import com.express.tool.CheckCode;
import com.express.tool.Phone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import pojo.Express;

import java.util.List;

@RestController
@RequestMapping("/express")
public class ExpressController {

    @Autowired
    ExpressService expressService;

    @Autowired
    UserService userService;

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping(value = "/findall")
    public Result findAll() {
        List<Express> ExpressList = expressService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", ExpressList);
    }

    /**
     * 统计仓库中目前的货物总量(仓库里面有的货物)
     *
     * @return
     */
    @GetMapping("/sum")
    public Result getSUM() {
        Integer sum = expressService.getSUM();
        return new Result(true, StatusCode.OK, "查询成功", sum);
    }

    /**
     * 通过输入快递编号 获取快递取货码
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/findCheckCode/{id}")
    public Result findCheckCode(@PathVariable("id") Integer id) {
        // 通过快递id查询到的对应取货码
        Integer checkCode = expressService.findCheckCodeById(id);

        // 通过快递id查询到对应的用户的手机号
        String phoneNum = userService.findPhoneNumberById(id);

        // 利用阿里大于的官方API发送短信，传入的两个参数分别为手机号，和取货码
        Phone.SendMSG(phoneNum, checkCode.toString());
        return new Result(true, StatusCode.OK, "验证码已经发送");
    }

    /**
     * 根据快递id查询数据
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Express express = expressService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", express);
    }

    /**
     * 查询所有超时的快递的信息
     * @return
     */
    @GetMapping("/findOutTimeAll")
    public Result findOutTimeAll() {
        List<Express> expressList = expressService.findOutTimeAll();
        return new Result(true, StatusCode.OK, "查询成功", expressList);
    }

    /**
     * 添加数据
     *
     * @param express
     */
    @Transactional
    @PostMapping("/add")
    public Result add(@RequestBody Express express) {
        // 在进行数据的添加之前，我们要先对该数据的生成一个验证码
        Integer checkCode = CheckCode.randomCode();
        // 将生成的验证码设置进该对象中
        express.setCheckCode(checkCode);
        expressService.add(express);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 更新操作
     *
     * @param express
     * @param id
     * @return
     */
    @Transactional
    @PutMapping("/update/{id}")
    public Result update(@RequestBody Express express, @PathVariable("id") Integer id) {
        express.setId(id);
        expressService.update(express);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @Transactional
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        expressService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 根据输入的取件验证码进行取件
     *
     * @param checkCode
     * @return
     */
    @Transactional
    @DeleteMapping("/delete/{checkCode}")
    public Result deleteByCheckCode(@PathVariable("checkCode") Integer checkCode) {
        expressService.deleteByCheckCode(checkCode);
        return new Result(true, StatusCode.OK, "取件成功");
    }

}

